from typing import *


class Solution:
    def maximizeSquareArea(
        self, m: int, n: int, hFences: List[int], vFences: List[int]
    ) -> int:

        def f(arr, limit):
            if 1 not in arr:
                arr.append(1)
            if limit not in arr:
                arr.append(limit)
            arr.sort()
            s = set()
            for i in range(len(arr)):
                for j in range(i + 1, len(arr)):
                    s.add(arr[j] - arr[i])
            return s

        s1 = f(hFences, m)
        s2 = f(vFences, n)
        ans = -1
        for s in s1:
            if s in s2:
                ans = max(ans, s)
        return ans * ans % 1000000007 if ans != -1 else -1
